using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Factories;
using System.IO;
namespace SimpleGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //The physics simulator is always needed. We supply a gravity of 250 units
        PhysicsSimulator simulator = new PhysicsSimulator(new Vector2(0, 500));

        //All that is needed to create and draw a rectangle
        //Body rectangleBody;
        //Geom rectangleGeom;
        //Texture2D rectangleTexture;
        //Vector2 rectangleOrigin;
        Sprite groundSprite;
        Ball circleSprite;

        //All that is needed to create and draw a circle
        //Body circleBody;
        //Geom circleGeom;
        //Texture2D circleTexture;
        //Vector2 circleOrigin;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Create a sprite
            groundSprite = new Sprite();
            groundSprite.Position = new Vector2(400, 500);
            groundSprite.Load("Ground", Content, simulator);
            groundSprite.IsStatic = true;
            groundSprite.Geom.OnCollision += OnCollision;

            circleSprite = new Ball();
            circleSprite.Position = new Vector2(400, 300);
            circleSprite.Load("Circle", Content, simulator);
            circleSprite.Geom.OnCollision += OnCollision;
            circleSprite.Speed = 1001.0f;




            //Load the circle texture
            //circleTexture = Content.Load<Texture2D>("Circle");
            //circleOrigin = new Vector2(circleTexture.Width / 2, circleTexture.Height / 2);

            //Create the circle body
            //circleBody = BodyFactory.Instance.CreateCircleBody(circleTexture.Width / 2, 1);
            //circleBody.Position = new Vector2(400, 300);
            //simulator.Add(circleBody);

            //Create the circle geometry
            //circleGeom = GeomFactory.Instance.CreateCircleGeom(circleBody, circleTexture.Width / 2, 48);
            //circleGeom.RestitutionCoefficient = 0.3f;
            //circleGeom.FrictionCoefficient = 0.5f;
            //simulator.Add(circleGeom);

            // Read from file
            using (StreamReader sr = new StreamReader("map1.txt"))
            {
                while (!sr.EndOfStream)
                {
                    int line = sr.Read();
                    int[] intArray;




                }
            }
        }
                    
                
            
             

        
        public bool OnCollision(Geom g1, Geom g2, ContactList contactList)
        {
            Sprite s1 = (Sprite)g1.Tag;
            Sprite s2 = (Sprite)g2.Tag;
            

            //Get the type
            //
            //if (g1.Tag.GetType() == typeof(Sprite))
            //{
            //    //now cast to what we want
            //}
            return true; 
        }
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            KeyboardState s = Keyboard.GetState();

            //We make it possible to rotate the circle body
            if (s.IsKeyDown(Keys.Space))
                circleSprite.Body.ApplyForce(new Vector2(0.0f, -circleSprite.Speed));
            if (s.IsKeyDown(Keys.A))
                circleSprite.Body.ApplyForce(new Vector2(-circleSprite.Speed, 0.0f));

            if (s.IsKeyDown(Keys.D))
                circleSprite.Body.ApplyForce(new Vector2(circleSprite.Speed, 0.0f));
            if (s.IsKeyDown(Keys.S))
                circleSprite.Body.ApplyForce(new Vector2(0.0f, circleSprite.Speed));
          

            //We update the physics simulator
            simulator.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);

            groundSprite.Update(gameTime);
            circleSprite.Update(gameTime);
            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            //We draw the two textures where the physics geometries are
            //spriteBatch.Draw(rectangleTexture, rectangleGeom.Position, null, Color.White, rectangleGeom.Rotation, rectangleOrigin, 1, SpriteEffects.None, 1);
            //spriteBatch.Draw(circleTexture, circleGeom.Position, null, Color.White, circleGeom.Rotation, circleOrigin, 1, SpriteEffects.None, 1);
            spriteBatch.Draw(groundSprite.CurrentTexture, groundSprite.Position, null, Color.White, groundSprite.Rotation, groundSprite.Origin, 1, SpriteEffects.None, 1);
            spriteBatch.Draw(circleSprite.CurrentTexture, circleSprite.Position, null, Color.White, circleSprite.Rotation, circleSprite.Origin, 1, SpriteEffects.None, 1);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
